<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/10/1
 * Time: 6:34 PM
 */

namespace app\api\controller\jiaoyi;

use app\api\model\JiaoyiUinfo;
use app\api\model\User as userinfo;
use think\Controller;
use think\Db;
use think\Request;

class Changwaiuinfo extends Controller
{
    /*
     * 检查用户是否实名，交易所入口检测
     *
     * @uname  用户名
     */
    public function ifyuname()
    {

        $jiaoyiuinfo0bj = new JiaoyiUinfo();

        $userinfoobj = new userinfo();

        $res = Request::instance();

        if ($res->isPost()) {

            if ($jiaoyiuinfo0bj->where('uname', $res->param('uname'))->value('shiming') == 1) {

                echo json_encode(['zt' => 1, 'msg' => '已更新过!']);

                die();

            }
            if (!$userinfoobj->where('uname', $res->param('uname'))->value('shiming') == 1) {

                echo json_encode(['zt' => 1, 'msg' => '您还没有实名,请先实名!']);

                die();

            }


            if ($jiaoyiuinfo0bj->insert([

                'uname' => $res->param('uname'),

                'uid' => $res->param('id'),

                'truename' => $userinfoobj->where('uname', $res->param('uname'))->value('truename'),

                'unickname' => $res->param('nicename'),

                'user_status' => 1,

                'shiming' => 1,

                'touxiang' => $res->param('headimg')

            ])) {

                echo json_encode(['zt' => 1, 'msg' => '信息同步成功']);

            } else {

                echo json_encode(['zt' => 0, 'msg' => '用户信息同步失败']);

            }

        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }

    }

    //收款码上传
    public function shoukuanimg_upload()
    {

        $file = request()->file('file');

        $info = $file->validate(['size' => 156780, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/jiaoyi');


        if ($info) {

            echo json_encode(['zt' => 1, 'msg' => $info->getSaveName()]);

        } else {

            echo json_encode(['zt' => 0, 'msg' => '上传失败']);

        }
    }

    //获取当前用户的基础信息，初始化前端
    public function Get_now_user_info()
    {

        $jiaoyiuinfo0bj = new JiaoyiUinfo();

        $res = Request::instance();

        if ($res->isPost()) {

            $data = $jiaoyiuinfo0bj->where('uname', $res->param('uname'))->column('uname,truename,unickname,uid,zhifubaozt,weixinzt,yinlianzt,weixinaccount,weixinimg,zhifubaoimg,zhifubaoaccount,bankname,bankkaihu,bankkaihuaddr')[$res->param('uname')];

            if ($data){

                echo json_encode(['zt'=>1,'mgs'=>'用户信息获取成功','data'=>$data]);

            }else{

                echo json_encode(['zt'=>0,'msg'=>'用户信息获取失败']);

            }

        } else {

            echo json_encode(['zt' => 0, 'msg' => 'error']);

        }

    }

    //更新收款信息
    public function Update_user_info()
    {

        $jiaoyiuinfo0bj = new JiaoyiUinfo();

        $res = Request::instance();

        if ($res->isPost()) {

            if ($res->param('type') == 'zhifubao') {

                if (Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->update(

                    [

                        'zhifubaoaccount' => $res->param('account'),

                        'zhifubaoimg' => $res->param('zhifubaoimg'),

                        'zhifubaozt' => $res->param('checked')

                    ]

                )) {

                    echo json_encode(['zt' => 1, 'msg' => '支付宝修改成功']);

                } else {

                    echo json_encode(['zt' => 0, 'msg' => '没有任何变动信息']);

                }

            } elseif ($res->param('type') == 'weixin') {

                if (Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->update(

                    [

                        'weixinaccount' => $res->param('account'),

                        'weixinimg' => $res->param('weixinimg'),

                        'weixinzt' => $res->param('checked')

                    ]

                )) {

                    echo json_encode(['zt' => 1, 'msg' => '微信修改成功']);

                } else {

                    echo json_encode(['zt' => 0, 'msg' => '没有任何变动信息']);

                }

            } elseif ($res->param('type') == 'yinlian') {

                if (Db::name('jiaoyi_uinfo')->where('uname', $res->param('uname'))->update(

                    [

                        'bankname' => $res->param('account'),

                        'bankkaihu' => $res->param('kaihuhang'),

                        'bankkaihuaddr' => $res->param('kaihuhangaddr'),

                        'yinlianzt' => $res->param('checked')

                    ]

                )) {

                    echo json_encode(['zt' => 1, 'msg' => '银行卡信息修改成功']);

                } else {

                    echo json_encode(['zt' => 0, 'msg' => '没有任何变动信息']);

                }

            } else {

                echo json_encode(['zt' => 0, 'msg' => '信息更新失败']);

            }

        } else {

            echo json_encode(['zt' => 2, 'msg' => 'error']);

        }

    }

}